package com.spic.business.hidden.governance.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.common.core.workflow.IWorkFlowService;
import com.spic.business.common.core.workflow.WorkFlowController;
import com.spic.business.hidden.governance.domain.HiddenGovernance;
import com.spic.business.hidden.governance.domain.vo.HiddenStandingVo;
import com.spic.business.hidden.governance.service.IHiddenGovernanceService;
import com.spic.business.hidden.place.domain.HiddenPlace;
import com.spic.business.hidden.place.service.IHiddenPlaceService;
import com.spic.business.hidden.type.domain.HiddenType;
import com.spic.business.hidden.type.service.IHiddenTypeService;
import com.spic.business.xfalarm.DPage;
import com.spic.common.core.constant.HttpStatus;
import com.spic.common.core.utils.ServletUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 隐患排查治理Controller
 *
 * @author renxiang
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/hidden/hidden_governance")
public class HiddenGovernanceController extends WorkFlowController
{
    @Autowired
    private IHiddenGovernanceService hiddenGovernanceService;
    @Autowired
    private IHiddenPlaceService hiddenPlaceService;

    @Autowired
    private IHiddenTypeService hiddenTypeService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询隐患排查治理列表
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:list")
    @GetMapping("/list")
    public TableDataInfo list(HiddenGovernance hiddenGovernance)
    {
        //获取部门及子部门
        if (StringUtils.isNotEmpty(hiddenGovernance.getRectificationDept())){
            List<Long> rectificationDepts = remoteDeptService.getDeptAndChildren(Long.parseLong(hiddenGovernance.getRectificationDept())).getData();
            hiddenGovernance.setRectificationDepts(rectificationDepts);
        }
        startPage();
        List<HiddenGovernance> list = hiddenGovernanceService.selectHiddenGovernanceList(hiddenGovernance);
        return getDataTable(list);
    }

    /**
     * 导出隐患排查治理列表
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:export")
    @Log(title = "隐患排查治理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiddenGovernance hiddenGovernance) throws IOException
    {
        List<HiddenGovernance> list = hiddenGovernanceService.selectHiddenGovernanceList(hiddenGovernance);
        ExcelUtil<HiddenGovernance> util = new BusinessExcelUtil<HiddenGovernance>(HiddenGovernance.class);
        util.exportExcel(response, list, "隐患排查治理数据");
    }

    /**
     * 获取隐患排查治理详细信息
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(hiddenGovernanceService.selectHiddenGovernanceById(id));
    }

    @GetMapping(value = "/info/{id}")
    public HiddenGovernance getHiddenGovernance(@PathVariable("id") Integer id)
    {
       return hiddenGovernanceService.selectHiddenGovernanceById(id);
    }
    /**
     * 新增隐患排查治理
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:add")
    @Log(title = "隐患排查治理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiddenGovernance hiddenGovernance)
    {
        return AjaxResult.success(hiddenGovernanceService.insertHiddenGovernance(hiddenGovernance));
    }

    /**
     * 修改隐患排查治理
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:edit")
    @Log(title = "隐患排查治理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HiddenGovernance hiddenGovernance)
    {
        return toAjax(hiddenGovernanceService.updateHiddenGovernance(hiddenGovernance));
    }

    /**
     * 删除隐患排查治理
     */
    @PreAuthorize(hasPermi = "business/hidden:hidden_governance:remove")
    @Log(title = "隐患排查治理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(hiddenGovernanceService.deleteHiddenGovernanceByIds(ids));
    }

    @Override
    protected IWorkFlowService getService()
    {
        return hiddenGovernanceService;
    }

    @GetMapping("/deadlineMessage")
    protected AjaxResult deadlineMessage(){
        hiddenGovernanceService.deadlineMessage();
        return AjaxResult.success();
    }

    @GetMapping("/getWeekHidden")
    public AjaxResult getWeekHidden(){
        List<Map<String,Object>> weekHiddenList =  hiddenGovernanceService.getWeekHidden();
        return AjaxResult.success(weekHiddenList);
    }

    //@PreAuthorize(hasPermi = "business/hidden:hidden_governance:list")
    @GetMapping("/hidden/standing/list")
    public TableDataInfo hiddenStandingList(HiddenStandingVo hiddenStandingVo)
    {
        Integer pageNum = ServletUtils.getParameterToInt("pageNum");
        Integer pageSize = ServletUtils.getParameterToInt("pageSize");
        List<HiddenStandingVo> list = hiddenGovernanceService.selectHiddenStandingList(hiddenStandingVo);
        if(StringUtils.isNotEmpty(hiddenStandingVo.getYear())){
            list = list.stream().filter(f -> f.getYear().equals(hiddenStandingVo.getYear())).collect(Collectors.toList());
        }
        if(StringUtils.isNotEmpty(hiddenStandingVo.getMouth())){
            list = list.stream().filter(f -> f.getMouth().equals(hiddenStandingVo.getMouth())).collect(Collectors.toList());
        }

        DPage<HiddenStandingVo> page = new DPage<>(list,pageNum,pageSize);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @PostMapping("/exportStanding")
    public void exportStanding(HttpServletResponse response, HiddenStandingVo hiddenStandingVo) throws IOException
    {
        List<HiddenStandingVo> hiddenStandingVoList = hiddenGovernanceService.selectHiddenStandingList(hiddenStandingVo);
        List<HiddenStandingVo> collect = hiddenStandingVoList.stream().filter(f -> f.getFileName().equals(hiddenStandingVo.getFileName())).collect(Collectors.toList());
        List<HiddenGovernance> hiddenGovernanceList = collect.get(0).getHiddenGovernanceList();
        hiddenGovernanceList.forEach(info->{
            if(StringUtils.isNotEmpty(info.getHiddenPlace())){
                HiddenPlace h1 = hiddenPlaceService.selectHiddenPlaceById(info.getHiddenPlace());
                info.setHiddenPlaceName(h1.getPlaceName());
            }
            if(StringUtils.isNotEmpty(info.getHiddenPlaceAdder())){
                HiddenPlace h2 = hiddenPlaceService.selectHiddenPlaceById(info.getHiddenPlaceAdder());
                String hiddenPlaceName = info.getHiddenPlaceName();
                info.setHiddenPlaceName(hiddenPlaceName+"-"+h2.getPlaceName());
            }

            if(StringUtils.isNotEmpty(info.getHazardType())){
                HiddenType hiddenType = hiddenTypeService.selectHiddenTypeById(info.getHazardType());
                info.setHazardTypeName(hiddenType.getHiddenTypeName());
            }

        });
        ExcelUtil<HiddenGovernance> util = new BusinessExcelUtil<HiddenGovernance>(HiddenGovernance.class);
        util.exportExcel(response, hiddenGovernanceList, hiddenStandingVo.getFileName());

    }

}
